package com.bulkmail.sender;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class WriteXMLOptionFile {

	private int emails;
	private int seconds;
	private boolean addCount;
	private boolean random;
	private String attachment;
	private boolean smtp;
	private boolean exs;
	private String path;

	public WriteXMLOptionFile(DataSession d, String path) {
		this.emails = d.getNrEmails();
		this.seconds = d.getSeconds();
		this.addCount = d.getAddCounter();
		this.random = d.getAddRandomSubject();
		this.attachment = d.getAttachPath();
		this.smtp = d.getSmtp();
		this.exs = d.getExs();
		this.path = path;
	}

	public void writeXMLOptionFile() {

		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.newDocument();

			Element bean = doc.createElement("bean");
			doc.appendChild(bean);
			Attr attr = doc.createAttribute("id");
			attr.setValue("multiplication");
			bean.setAttributeNode(attr);

			Element property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("Number of emails");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("value");
			attr.setValue(String.valueOf(emails));
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("Time between emails");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("value");
			attr.setValue(String.valueOf(seconds));
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("key");
			attr.setValue("add counter");
			property.setAttributeNode(attr);
			property.appendChild(doc.createTextNode(String.valueOf(addCount)));

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("key");
			attr.setValue("add random subject");
			property.setAttributeNode(attr);
			property.appendChild(doc.createTextNode(String.valueOf(random)));

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("name");
			attr.setValue("attachment");
			property.setAttributeNode(attr);
			attr = doc.createAttribute("value");
			attr.setValue(attachment);
			property.setAttributeNode(attr);

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("key");
			attr.setValue("Smtp");
			property.setAttributeNode(attr);
			property.appendChild(doc.createTextNode(String.valueOf(smtp)));

			property = doc.createElement("property");
			bean.appendChild(property);
			attr = doc.createAttribute("key");
			attr.setValue("Exchange server");
			property.setAttributeNode(attr);
			property.appendChild(doc.createTextNode(String.valueOf(exs)));

			// write the content into xml file
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(new File(path));

			transformer.transform(source, result);

			System.out.println("File saved!");

		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		}

	}
}
